//
//  MPlusTabBarController.m
//  medicineplus
//
//  Created by gufei on 15-5-14.
//  Copyright (c) 2015年 chn_ruby@126.cn. All rights reserved.
//

#import "MPlusTabBarController.h"
#import "MPlusNavigationController.h"
#import "MPlusMainViewController.h"
#import "MPlusNewsViewController.h"
#import "MPlusEduViewController.h"
#import "MPlusMyClassViewController.h"
#import "MPlusUserViewController.h"
#import "MPlusUtils.h"
#import "MPlusDefine.h"
#import "MPlusString.h"

@implementation MPlusTabBarController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        [self.view setBackgroundColor:[UIColor clearColor]];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    CGRect frame = CGRectMake(0.0, 0.0, self.view.bounds.size.width, TABBAR_HEIGHT);
    UIView *view = [[UIView alloc] initWithFrame:frame];
    [view setBackgroundColor:[UIColor whiteColor]];
//    [view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:kTabBarBackground]]];
//    [view setAlpha:0.9];
    [self.tabBar addSubview:view];
    
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[MPlusUtils colorWithHexString:kGrayColor], NSForegroundColorAttributeName,nil] forState:UIControlStateNormal];
    
    [[UITabBarItem appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:[MPlusUtils colorWithHexString:kBlueColor],NSForegroundColorAttributeName,nil]forState:UIControlStateSelected];
    
    [[UITabBarItem appearance] setTitlePositionAdjustment:UIOffsetMake(0, -3)];
    
    MPlusMainViewController *mainCtrol = [[MPlusMainViewController alloc]initWithNibName:nil bundle:nil];
    MPlusNewsViewController *newsCtrol = [[MPlusNewsViewController alloc]initWithNibName:nil bundle:nil];
    MPlusEduViewController *eduCtrol = [[MPlusEduViewController alloc]initWithNibName:nil bundle:nil];
    MPlusMyClassViewController *myClassCtrol = [[MPlusMyClassViewController alloc]initWithNibName:nil bundle:nil];
    MPlusUserViewController *userCtrol = [[MPlusUserViewController alloc]initWithNibName:nil bundle:nil];
    
    NSArray *viewCtrols = @[mainCtrol, newsCtrol, eduCtrol, myClassCtrol, userCtrol];
    NSMutableArray *viewCotrollers = [NSMutableArray arrayWithCapacity:5];
    for (int i=0; i<[viewCtrols count]; i++) {
        MPlusNavigationController *nav = [[MPlusNavigationController alloc] initWithRootViewController:viewCtrols[i]];
        [viewCotrollers addObject:nav];
    }
    self.viewControllers = viewCotrollers;
    self.selectedIndex = 0;
}

- (void)setSelectedIndex:(NSUInteger)index
{
    [super setSelectedIndex:index];
//    [self setNoHighlightTabBar];
}

#pragma
#pragma mark - NoHighlightTab
- (void)setSelectedViewController:(UIViewController *)selectedViewController
{
    [super setSelectedViewController:selectedViewController];
//    [self setNoHighlightTabBar];
}

- (void)setNoHighlightTabBar
{
    int tabCount = [self.viewControllers count] > 5 ? 5 : [self.viewControllers count];
    NSArray * tabBarSubviews = [self.tabBar subviews];
    for(int i = [tabBarSubviews count] - 1; i > [tabBarSubviews count] - tabCount - 1; i--)
    {
        for(UIView * v in [[tabBarSubviews objectAtIndex:i] subviews])
        {
            if(v && [NSStringFromClass([v class]) isEqualToString:@"UITabBarSelectionIndicatorView"])
            {//the v is the highlight view.
                [v removeFromSuperview];
                break;
            }
        }
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
